//
//  SearchBarTableView.m
//  B段项目
//
//  Created by dllo on 15/9/25.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "SearchBarTableView.h"
#import "PrefixHeader.pch"
#import "SearchCell.h"
#import "AFNetWorkingW.h"
#import "SearchBarModel.h"
#import <UIImageView+WebCache.h>

@interface SearchBarTableView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *mArr;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *carImageView;
@property (nonatomic, strong) UIActivityIndicatorView *activity;
@property (nonatomic, strong) SearchBarModel *searchBarModel;

@end

@implementation SearchBarTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.backgroundColor = [UIColor whiteColor];
        [self registerClass:[SearchCell class] forCellReuseIdentifier:@"reuse"];
        
        NSNotificationCenter *searchCenter = [NSNotificationCenter defaultCenter];
        [searchCenter addObserver:self selector:@selector(passSearchUrl:) name:@"passSearchUrl" object:nil];
        
        self.mArr = [NSMutableArray array];
        
        self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//        self.activity.center = self.center;
        self.activity.frame = CGRectMake((ScreenWidth - 80) / 2, (ScreenHeight - 120) / 2, 80, 80);
        self.activity.color = RGB(22, 188, 180);
        [self addSubview:self.activity];
        [self.activity startAnimating];
        


    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mArr.count;
    
}

#pragma mark --row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreenHeight * 80 / 667;
}

#pragma mark --cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell.searchBarModel = self.mArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      NSNotificationCenter *webCenter = [NSNotificationCenter defaultCenter];
    
    self.searchBarModel = [[SearchBarModel alloc] init];
    
    self.searchBarModel = self.mArr[indexPath.row];
    
    [webCenter postNotificationName:@"passWebUrl" object:self.searchBarModel.newsLink];
}

- (void)passSearchUrl:(NSNotification *)noti
{
    NSString *url = [noti.object stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *urlstring = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/searchNews.php?page=1&searchContent=%@",url];
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:urlstring resultBlock:^(id result) {
        if ([result objectForKey:@"hasMoreNews"] == [NSNumber numberWithInteger:0]) {
            [UIView animateWithDuration:1 animations:^{
                [self imageViewSubviews];
            }];
        }else{
            NSArray *arr = [result objectForKey:@"newsList"];
            for (NSDictionary *dic in arr) {
            SearchBarModel *model = [[SearchBarModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.mArr addObject:model];
        }
            [self reloadData];
            [self.activity stopAnimating];
        }
    }];
}

- (void)imageViewSubviews
{
#pragma mark --添加搜索不到后的图片
    self.carImageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth * (375 - 170) / 2 / 375, ScreenHeight * 160 / 667, ScreenWidth * 180 / 375, ScreenHeight * 140 / 667)];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth * (375 - 170) / 2 / 375, ScreenHeight * 310 / 667, ScreenWidth * 180 / 375, ScreenHeight * 140 / 667)];
    
    self.imageView.image = [UIImage imageNamed:@"meizhaodao.png"];
    
    [self.carImageView sd_setImageWithURL:[NSURL URLWithString:@"http://pic.nipic.com/2007-10-26/2007102616643238_2.gif"] placeholderImage:[UIImage imageNamed:@"meizhaodao.png"]];
    
    [self addSubview:self.carImageView];
    [self addSubview:self.imageView];
}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
